import axios from '@/libs/api.request'

export default {
  storeName: 'userRoleStore',
  namespaced: true,
  state: {

  },
  mutations: {

  },
  actions: {
    loadUserRoleList ({ state, commit }, { pageIndex, pageSize }) {
      return axios.request({
        url: '/api/OauthRole/list',
        params: {
          pageIndex,
          pageSize
        },
        method: 'get'
      })
    },
    loadUserRoleAll ({ state, commit }) {
      return axios.request({
        url: '/api/OauthRole/all',
        method: 'get'
      })
    },
    listRoleResourcs ({ state, commit }, { clientId, roleId }) {
      return axios.request({
        url: '/api/OauthRole/resourcs',
        params: { clientId, roleId },
        method: 'get'
      })
    },
    save ({ state, commit }, vo) {
      const requestUrl = (vo.roleId && vo.roleId !== '') ? '/api/OauthRole/update' : '/api/OauthRole/add'
      return axios.request({
        url: requestUrl,
        method: 'post',
        data: vo
      })
    },
    saveResources ({ state, commit }, vo) {
      const requestUrl = '/api/OauthRole/saveResource'
      return axios.request({
        url: requestUrl,
        method: 'post',
        data: vo
      })
    },
    delete ({ state, commit }, {roleId}) {
      return axios.request({
        url: 'api/OauthRole/delete',
        method: 'post',
        data: {id: roleId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    }
  }
}
